﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class PushController : ControllerBase
    {
        public PushApplication pushApplication { get; set; }
        public ILogger<PushController> _logger { get; set; }

        /// <summary>
        /// 绑定个推ID  未绑定过个推ID/活个推ID变更  需要重新绑定
        /// </summary>
        /// <param name="geTuiID">个推ID</param>
        /// <returns></returns>
        [HttpPost, TokenValidFilter]
        public ActionResult BindGeTuiID([FromForm] string geTuiID)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                var uid = User.Claims.First(i => i.Type == "ID").Value;
                var r = pushApplication.BindGeTuiRelation(int.Parse(uid), geTuiID);

                return this.Success(r);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }
    }
}
